package com.cl.code.database.mybatis.module.sql.table;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author chengliang
 * @since 1.0.0
 */
public class TableInfoFactory {

    private final static Map<Class<?>, TableInfo> cache = new ConcurrentHashMap<>(20);

    public static TableInfo getTableInfo(Class<?> entityClass) {
        TableInfo tableInfo = cache.get(entityClass);
        if (tableInfo == null) {
            tableInfo = createTableInfo(entityClass);
            cache.put(entityClass, tableInfo);
        }
        return tableInfo;
    }

    private static TableInfo createTableInfo(Class<?> entityClass) {

        String tableName = TableInfoParser.parseTableName(entityClass);

        TableField[] tableFields = TableInfoParser.parseTableField(entityClass);

        if (tableFields.length == 0) {
            throw new IllegalArgumentException("Table fields cannot be empty");
        }
        return new TableInfo(entityClass, tableName, tableFields);
    }

}
